import axios from 'axios'
import Cookie from 'vue-cookies'
import router from '../router'
import store from '../store'
import {
	encrypt,
	decrypt
} from '@/utils/crypto'
import utils from '@/utils'
import {
	userYklogin
} from '@/api/home'
import DonMessage from '@/utils/message'
let promise;
export const HOST = process.env.NODE_ENV === 'production' ? '' : '/devApi'
var instance = axios.create({
	baseURL: HOST,
	responseType: 'json', // default
	responseEncoding: 'utf8' // default
});



// const AUTH_TOKEN = ''
// instance.defaults.headers['token'] = AUTH_TOKEN;
instance.interceptors.request.use(async function(config) {
		let token = localStorage.getItem('TOKEN')
		let lang = localStorage.getItem('lang') ? localStorage.getItem('lang') : utils.getUserLang()
		// let unit = localStorage.getItem('unit') ? localStorage.getItem('unit') : '';
		config.headers = {
			...config.headers,
			'Authorization': token ? token : '',
			lang: lang,
			Fingerprint: localStorage.getItem('Fingerprint'),
			// unit: unit || '',
		}
		// console.log('config', config);
		return config
	},
	function(error) {
		return Promise.reject(error);
	})

function jsonData(data) {
	try {
		return JSON.parse(decrypt(data))
	} catch {
		return data
	}
}

instance.interceptors.response.use(async function(response) {
	const {
		data
	} = response
	// console.log('data::::::', jsonData('3Fd/MVHjxR7eK0bnYKkGmEnTXA6LAFEzxJ9VUA9oBctdV3mgd60YwqbFKMlUGCbw2UnbSHqZY5kYuUvPosJ8T81RtfK2qadfekl8/XVw+RyXTH2Skl6aXGZpWOIGfJejEXLy1K8FcKwYcbnRgPsnIWGJj0+3DNKZIwlijPYvo5wp2jfHter+fS2MDp3HbnnCE1v0TTAdjUsS37lhZOb66jLFlT3BqPRTuzrEthpLIMIjIgqroid4GITvYy6NAWqrwYY13LhlL+yehc8nYUZ8JUTLrH2iiktwp33n3MchheUF22XK4QD2/vkyGNEdQP1pANo6/ZEhQUnRC+9qiHyvOOTVm8LJ8MWfM0DqkCf8clOwHBuxZAaM/LM0tmDpD8NLhZlql42qW2HSRVMIKgP7FWRS9yn0bGPwu5blEM/DIUgPWW78rPsxE512Msq4onB66uZFKLzgUbarPbymY2q4Iw=='));
	if (data && data.code == 1 && decrypt(data.data)) {
		return {
			...data,
			data: jsonData(data.data)
		}
	} 
	// else if (data && data.code == 999) {
	// 	console.log('清空token1', data);
	// 	localStorage.clear('LOGIN_USER_INFO')
	// 	localStorage.clear('TOKEN')
	// 	if (store.state.windowWidth > 1024) {
	// 		router.replace('/')
	// 	} else {
	// 		router.replace('/mobile/login')
	// 	}
	// 	// router.replace('/')
	// 	return data
	// } 
	else if (data && data.code == 405) {
		return data
	} else {
		return data || response
	}
}, async function(error) {
	// console.log('error', error);
	if (error.response && error.response.status == 999 && error.response.data.code == 999) {
		console.log('清空token2', error);
		localStorage.clear('LOGIN_USER_INFO')
		localStorage.clear('TOKEN')
		if (store.state.windowWidth > 1024) {
			router.replace('/')
		} else {
			router.replace('/mobile/login')
		}
		// router.replace('/')
		DonMessage.error(error.response.data.msg)
	} else if (error.response && error.response.status == 405 && error.response.data.code == 405) {
		// DonMessage.error('')
	} else {
		let lang = localStorage.getItem('lang') ? localStorage.getItem('lang') : utils.getUserLang()
		let msg = error.message
		if (error.message === 'Network Error') {
			msg = lang == 'zh' ? '网络错误' : lang == 'cht' ? '網絡錯誤' : error.message
		}
		DonMessage.error(msg)
	}
	return Promise.reject(error);
})

export default instance;